# How Does Improvement in Commuting Affect Employees? Evidence from a Natural Experiment
# Version: 20211014

## Yao Lu (luyao@sem.tsinghua.edu.cn),        Tsinghua University
## Xinzheng Shi (shixzh@sem.tsinghua.edu.cn), Tsinghua University
## Jagadeesh Sivadasan (jagadees@umich.edu),  University of Michigan
## Zhufeng Xu (zhufeng@zhufengxu.com),        Central University of Finance and Economics

if (!exists("AllScriptFlag", 1)) {
  source("./Scripts/1Header.R")
}

Swipe <- read_dta("Data/Swipe.dta")
midnightmin_cutoff <- read_dta("Data/midnightmin_cutoff.dta")

Swipe <- mutate(Swipe, Date = as.Date(Date, origin = "1960-01-01"))
Swipe <- mutate(Swipe, `Subway opened` = factor(Post, c(0, 1), c("Before", "After")))

Plot_TimeofArrivalAndLeaving(plot_data = Swipe %>% filter(NearSubway == 1), 
                             data = midnightmin_cutoff, type = "Arrival")
ggsave("Output/Figure3Ai_TimeofArrival_Affected.png", width = 6, height = 4.944, unit = "in")
ggsave("Output/Figure3Ai_TimeofArrival_Affected.pdf", width = 6, height = 4.944, unit = "in", device = cairo_pdf)

Plot_TimeofArrivalAndLeaving(plot_data = Swipe %>% filter(NearSubway == 0), 
                             data = midnightmin_cutoff, type = "Arrival")
ggsave("Output/Figure3Aii_TimeofArrival_Unaffected.png", width = 6, height = 4.944, unit = "in")
ggsave("Output/Figure3Aii_TimeofArrival_Unaffected.pdf", width = 6, height = 4.944, unit = "in", device = cairo_pdf)

Plot_TimeofArrivalAndLeaving(plot_data = Swipe %>% filter(NearSubway == 1), 
                             data = midnightmin_cutoff, type = "Leaving")
ggsave("Output/Figure3Bi_TimeofLeaving_Affected.png", width = 6, height = 4.944, unit = "in")
ggsave("Output/Figure3Bi_TimeofLeaving_Affected.pdf", width = 6, height = 4.944, unit = "in", device = cairo_pdf)

Plot_TimeofArrivalAndLeaving(plot_data = Swipe %>% filter(NearSubway == 0), 
                             data = midnightmin_cutoff, type = "Leaving")
ggsave("Output/Figure3Bii_TimeofLeaving_Unaffected.png", width = 6, height = 4.944, unit = "in")
ggsave("Output/Figure3Bii_TimeofLeaving_Unaffected.pdf", width = 6, height = 4.944, unit = "in", device = cairo_pdf)

rm(Swipe, midnightmin_cutoff)
